package com.tiantianbook.mvpretrofitrxjavademo.view.main;

import android.app.ProgressDialog;
import android.widget.TextView;

import com.tiantianbook.mvpretrofitrxjavademo.R;
import com.tiantianbook.mvpretrofitrxjavademo.base.BaseActivity;
import com.tiantianbook.mvpretrofitrxjavademo.bean.Result;
import com.tiantianbook.mvpretrofitrxjavademo.presenter.MvpPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class MainActivity extends BaseActivity<MvpView, MvpPresenter> implements MvpView {

    @BindView(R.id.tv_main_content)
    TextView tvMainContent;
    private ProgressDialog dialog;

    @Override
    protected MvpPresenter initPresenter() {
        return new MvpPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tvMainContent.setText("asdfasdfadsfafd");
        dialog = new ProgressDialog(this);
        dialog.setMessage("数据加载中...");
    }

    @Override
    protected void initData() {
       mPresenter.getData();
    }


    @Override
    public void beforeRequest() {
        dialog.show();
    }

    @Override
    public void afterRequest(String result) {
        dialog.dismiss();
        tvMainContent.setText(result);
    }

    private void singleMap() {
        Observable<Result> observable = Observable.just(new Result("1"), new Result("2"), new Result("3"));
        observable.observeOn(AndroidSchedulers.mainThread())//指定回调线程
                .subscribeOn(Schedulers.io())//指定逻辑线程
                .map(new Func1<Result, String>() {
                    @Override
                    public String call(Result result) {
                        return result.getResultcode();
                    }
                }).subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                tvMainContent.append(s + "\n");
            }
        });
    }

    private void flatMap() {
        List<String> data1 = new ArrayList<>();
        data1.add("1");
        List<String> data2 = new ArrayList<>();
        data1.add("1");
        data1.add("2");
        List<String> data3 = new ArrayList<>();
        data1.add("1");
        data1.add("2");
        data1.add("3");
        Result result1 = new Result(data1);
        Result result2 = new Result(data2);
        Result result3 = new Result(data3);

        Observable<Result> observable = Observable.just(result1, result2, result3);
        observable.flatMap(new Func1<Result, Observable<String>>() {
            @Override
            public Observable<String> call(Result result) {
                return Observable.from(result.getData());
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                tvMainContent.append(s + "  ");
            }
        });
    }
}
